package genomemap.map;

import genomemap.data.Chromosome;
import java.util.ArrayList;

import java.util.List;
import java.util.Set;

/**
 * 
 * @author Susanta Tewari 
 */
public class FullContigMap extends ContigMap {

    public FullContigMap(ProbeMap probeMap) {
        super(probeMap);
    }

    @Override
    protected List<Contig> buildContigMap() {

        List<Contig> result = new ArrayList<Contig>();

        Chromosome chromosome = Chromosome.getInstance(probeMap.getLinkageGroup());
        List<String> probes = probeMap.getProbes();

        Set<String> set1;
        Set<String> set2;
        List<String> allClones = new ArrayList<String>();

        Contig contig = new Contig();

        for (int i = 0; i < probes.size()-1; i++) {

            contig.addProbe(probes.get(i));
            allClones.add(probes.get(i));

            set1 = chromosome.getConnectingClones(probes.get(i));
            set2 = chromosome.getConnectingClones(probes.get(i),probes.get(i + 1));

            set1.removeAll(set2);
            set1.removeAll(allClones);
            set2.removeAll(allClones);

            contig.addClones(new ArrayList<String>(set1));
            contig.addClones(new ArrayList<String>(set2));

            allClones.addAll(set1);
            allClones.addAll(set2);

            if (set2.isEmpty()) {
                result.add(contig);
                contig = new Contig();
            }

        }

        // last probe
        contig.addProbe(probes.get(probes.size()-1));
        set1 = chromosome.getConnectingClones(probes.get(probes.size()-1));
        set1.removeAll(allClones);
        contig.addClones(new ArrayList<String>(set1));

        result.add(contig);

        return result;

    }

}
